package com.ruoyi.web.controller.exam;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.exam.domain.PQuestion;
import com.ruoyi.exam.domain.PQuestionBank;
import com.ruoyi.exam.service.IPQuestionService;
import com.ruoyi.exam.vo.AddExamByQuestionVo;
import com.ruoyi.exam.vo.PQuestionBankVo;
import com.ruoyi.exam.vo.QuestionListVo;
import com.ruoyi.exam.vo.QuestionVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试题Controller
 *
 * @author ruoyi
 * @date 2021-12-31
 */
@RestController
@RequestMapping("/system/question")
public class PQuestionController extends BaseController
{
    @Autowired
    private IPQuestionService pQuestionService;

    /**
     * 查询试题列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(PQuestion pQuestion)
    {
        startPage();
        List<QuestionListVo> list = pQuestionService.selectPQuestionListVo(pQuestion);
        return getDataTable(list);
    }

    /**
     * 导出试题列表
     */
    @PreAuthorize("@ss.hasPermi('system:question:export')")
    @Log(title = "试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PQuestion pQuestion)
    {
        List<PQuestion> list = pQuestionService.selectPQuestionList(pQuestion);
        ExcelUtil<PQuestion> util = new ExcelUtil<PQuestion>(PQuestion.class);
        util.exportExcel(response, list, "试题数据");
    }

    /**
     * 获取试题详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id, @RequestParam Boolean type)
    {
        return AjaxResult.success(pQuestionService.selectPQuestionById(id,type));
    }

    /**
     * 新增试题
     */
    @PreAuthorize("@ss.hasPermi('system:question:add')")
    @Log(title = "试题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PQuestion pQuestion)
    {
        return toAjax(pQuestionService.insertPQuestion(pQuestion));
    }

    /**
     * 修改试题
     */
    @PreAuthorize("@ss.hasPermi('system:question:edit')")
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PQuestion pQuestion)
    {
        return toAjax(pQuestionService.updatePQuestion(pQuestion));
    }

    /**
     * 删除试题
     */
    @PreAuthorize("@ss.hasPermi('system:question:remove')")
    @Log(title = "试题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pQuestionService.deletePQuestionByIds(ids));
    }


    @GetMapping("/getBQuestionGroupName")
    public AjaxResult getBQuestionGroupName() {
        PQuestion pQuestion = new PQuestion();
        pQuestion.setQuType(Long.valueOf(4));
        List<PQuestion> pQuestions = pQuestionService.selectPQuestionList(pQuestion);
        return AjaxResult.success(pQuestions);
    }

    /**
     * 新增考试
     */
    @PreAuthorize("@ss.hasPermi('system:bank:add')")
    @PostMapping("/add")
    public AjaxResult getAdd(@RequestBody QuestionVo questionVo)
    {

        return toAjax(pQuestionService.sevePQuestion(questionVo,getUserId().toString()));
    }


    /**
     * 修改考试
     */
    @PreAuthorize("@ss.hasPermi('system:bank:edit')")
    @PostMapping("/edit")
    public AjaxResult getEdit(@RequestBody QuestionVo questionVo)
    {
        return toAjax(pQuestionService.updatePQuestion(questionVo,getUserId().toString()));
    }



    /*
    * 获取B星题的内容赋值
     */
    @PostMapping("/getBType")
    public AjaxResult getBType(@RequestBody Long id)
    {

        return AjaxResult.success();
    }

    @GetMapping("/getTypes")
    public AjaxResult getTypes(){
        return pQuestionService.getTypes();
    }


}
